const mongoose = require("./database");

const UserSchema = new mongoose.Schema({
  name: String, //用户昵称
  password: String, //用户密码
  phone: String, //用户手机号
  studentId: String, //用户学号
  address: String, //用户地址
  img: String, //用户头像
  tag: String, //个性签名
  birthday: Date, //用户生日
  gender: String, //用户性别
  Xihuan:Number,  //被喜欢
  Pinglun:Number, //被评论
  Zhuanfa:Number, //被转发
  usernamexihuan: {    //用户喜欢的视频
    type:Array,
    default:[]
  },
  usernameGuanzhu: {
    type:Array,
    default:[]
  }
});

const UserModel = mongoose.model("User", UserSchema, "user");

const CoursesSchema = new mongoose.Schema({
  course: String, //课程名称
  minutes: Number, //课程时长
  teacher: String,//主讲人
  img: String,//视频封面
  video: {
    title: String, //视频标题
    url: String, //视频地址
    status: Number, //视频状态
    createdAt: Date, //创建时间
    updatedAt: Date, //更新时间
  },
});

const CoursesModel = mongoose.model("Courses", CoursesSchema, "courses");

const DataSchema = new mongoose.Schema({
  title: String, // 题目标题
  option: [
    {
      text: String, // 选项内容
      value: String, // 选项值
    },
  ],
  answer: String, // 正确答案
  status: {
    type: Number,
    default: 0, // 1:正确, 0:错误
  },
  state: {
    type: Number,
    default: 0, // 0:未完成, 1:已完成
  },
  courseId: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "Courses", // 关联到课程表
  },
});

const DataModel = mongoose.model("Data", DataSchema, "data");

// 精选

// 公开课
const kp = new mongoose.Schema({
  title: String, // 题目标题
  desc: String, //  内容
  img: {
    type: Array,
    default: [],
  }, //图片
  time: {
    //发布时间
    type: Date,
    default: Date.now,
  },
  dianjiliang: { type: Number, default: 0 },  //点击量
})

const kpModel = mongoose.model("kp", kp, "kp");

// 学习文章
const jx = new mongoose.Schema({
  title: String, // 题目标题
  desc: String, //  内容
  img: {
    type: Array,
    default: [],
  }, //图片
  time: {
    //发布时间
    type: Date,
    default: Date.now,
  },
  dianjiliang: { type: Number, default: 0 },  //点击量
});

const jxModel = mongoose.model("jx", jx, "jx");

// 圈子
const quanzi = new mongoose.Schema({
  userid: {
    //发布者
    type: mongoose.Schema.Types.ObjectId,
    ref: "User",
  },
  content: String, //发布内容
  img: {
    type: Array,
    default: [],
  }, //图片
  time: {
    //发布时间
    type: Date,
    default: Date.now,
  },
  comments: {
    type: Array,
    default: [],
  },
  like: {
    type: Boolean,
    default: false,
  },
  zf: { type: Number, default: 0 },
  pinglun: { type: Number, default: 0 },
  xihuan: { type: Number, default: 0 },
});

const quanziModel = mongoose.model("quanzi", quanzi, "quanzi");


// 评论
const pinglun = new mongoose.Schema({
  content:String,
  time:{
    type: Date,
    default: Date.now
  },
  quanziid: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "quanzi"
  },
  userId: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "User"
  }
});

const pinglunModel = mongoose.model("pinglun", pinglun, "pinglun");


// 个人中心（待定）

// 意见反馈
const yijianfankui = new mongoose.Schema({
  userid: {
    //反馈用户
    type: mongoose.Schema.Types.ObjectId,
    ref: "User",
  },
  content: String, //反馈内容
  dianhua: Number, //联系电话
});

const yijianfankuiModel = mongoose.model(
  "yijianfankui",
  yijianfankui,
  "yijianfankui"
);

// 积分商城数据
const jifenshangchengDate = new mongoose.Schema({
  name: String, //商品名称
  price: Number, //积分价格
  img: {      //商品图片
    type: String,
    default: null
  },
  num: {     //已兑换数量
    type: Number,
    default: 0
  },
  guige: {    //物件规格
    type: String,
    default: null
  },
  desc: {      //商品介绍
    type: String,
    default: null
  },
  quyiDesc: {
    type: String,
    default: null
  }, //使用规则
  stateTime: {    //生成时间
    type: Date,
    default: null,
  }
});

const jifenshangchengDateModel = mongoose.model(
  "jfDate",
  jifenshangchengDate,
  "jfDate"
);

// 订单
const wdDingdan = new mongoose.Schema({
  shangjiadesc: String, //给商家留言
  yunfei: Number, //运费
  status:{
    type:Number,
    default:0  // 0 1 2 3 分别表示全部 待发货 待收货 待评价
  },
  fahuocode: {
    //货物状态 fasle待发货 true发货
    type: Boolean,
    default: false,
  },
  shouhuocode: {
    //收货状态  fasle未收货  true确认收货
    type: Boolean,
    default: false,
  },
  pingjiacode: {
    //评价状态  fasle未评价  true已评价
    type: Boolean,
    default: false,
  },
  userDate: {
    //获取商品详细信息
    type: mongoose.Schema.Types.ObjectId,
    ref: "jfDate",
  },
  userid: {
    //所购用户
    type: mongoose.Schema.Types.ObjectId,
    ref: "User",
  },
});

const wdDingdanModel = mongoose.model("dingdan", wdDingdan, "dingdan");

// 下单信息(物流)
const wuliu = new mongoose.Schema({
  name: String,
  wuliugongsi: String,
  danhao: String,
  xiadanDate: {
    xiadanCode: {
      //下单状态  fasle未下单  true已下单
      type: Boolean,
      default: false,
    },
    xiadanDate: {
      //下单时间
      type: Date,
      default: null,
    },
  },
  dingdanfacode: {
    //发货状态  fasle未发货  true已发货
    type: Boolean,
    default: false,
  },
  dingdanpscode: {
    //派送状态  fasle未派送  true已派送
    type: Boolean,
    default: false,
  },
  userid: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "dingdan",
  },
});

const wuliuModel = mongoose.model("wuliu", wuliu, "wuliu");


//地址字段
const address = new mongoose.Schema({
  id: String,
  address: String,
  name: String,//联系人
  phone: String,//手机号
  tag: String,//地址标签
  longitude: Number,//经度
  latitude: Number,//纬度
  userid: String,
  isDefault: {
      type: Boolean,
      default: false
  }
})

const addressModel = mongoose.model('address', address, 'address')

//积分详情
const jifenxiang = new mongoose.Schema({
  title: {   //怎么获取的积分
    type: String,
    default: "暂无备注"
  },
  userid: { //用户是谁
    type: mongoose.Schema.Types.ObjectId,
    ref: "User",
  },
  oldjifen: Number,
  newjifen: Number,
  totaljifen: Number, // 添加总积分字段
  time: { //时间
    type: Date,
    default: Date.now
  }
});
const jifenxiangModel = mongoose.model('jifenxiang', jifenxiang, 'jifenxiang');

// 签到记录
const signin = new mongoose.Schema({
  userid: {
    type: mongoose.Schema.Types.ObjectId,  // 改为String类型，因为前端传来的是字符串
    required: true,
    ref: "User",
  },
  date: {
    type: Date,
    required: true
  },
  points: {
    type: Number,
    default: 2
  }
});

// 创建复合索引，确保每个用户每天只能签到一次
signin.index({ userid: 1, date: 1 }, { unique: true });
const signinModel = mongoose.model('signin', signin, 'signin');

// 导出模型
module.exports = {
  UserModel,//用户表
  CoursesModel,//课程表
  DataModel,//题目表
  yijianfankuiModel,//意见反馈表
  jifenshangchengDateModel,//积分商城表
  wdDingdanModel,//订单表
  wuliuModel,//物流表
  quanziModel,//圈子表
  kpModel,//精选表
  jxModel,//学习文章表
  addressModel,//地址表
  jifenxiangModel,//积分详情表
  signinModel,//签到记录表
  pinglunModel,//评论表
};
